package cn.yangyumanage;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.wang.avi.AVLoadingIndicatorView;

import cn.yangyumanage.bean.LoginInfo;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.LoginHttpModel;
import cn.yangyumanage.util.ToastUtil;
import cn.yangyumanage.util.ViewUtil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;

    private final LoginHttpModel httpModel = new LoginHttpModel();
    private AVLoadingIndicatorView aiv_login;
    private CheckBox cb_remember_password;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initSharedPreferences();
        initView();
        reload();
    }


    private void initSharedPreferences() {
        sharedPreferences = getSharedPreferences("loginConfig", Context.MODE_PRIVATE);//设置保存在私有目录
        editor = sharedPreferences.edit();
    }

    //加载账号密码
    private void reload() {
        boolean isRemember = sharedPreferences.getBoolean("remember_password", false);
        if (isRemember) {
            String username = sharedPreferences.getString("username", "");
            String password = sharedPreferences.getString("password", "");
            et_username.setText(username);
            et_password.setText(password);
            cb_remember_password.setChecked(true);
        }
    }

    private void initView() {
        statusBarSetting();
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    ViewUtil.showSoftInput(LoginActivity.this, et_password);
                }
            }
        });
        LinearLayout ll_login = findViewById(R.id.ll_login);
        ll_login.setOnClickListener(this);
        aiv_login = findViewById(R.id.aiv_login);
        cb_remember_password = findViewById(R.id.cb_remember_password);
    }

    //设置状态栏
    private void statusBarSetting() {
        Window window = getWindow();
        //设置状态栏颜色为透明
        window.setStatusBarColor(getResources().getColor(R.color.white, null));
        //设置状态栏字体颜色自行改变
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    //登录
    private void login(String username, String password) {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(username);
        loginInfo.setPassword(password);
        httpModel.login(loginInfo, new DataCallBack() {
            @Override
            public void getData(Object data) {
                int code = (int) data;
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (code == 200) {
                            ToastUtil.show(LoginActivity.this, "登录成功");
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            if (cb_remember_password.isChecked()) {
                                editor.putBoolean("remember_password", true);
                                editor.putString("username", username);
                                editor.putString("password", password);
                            } else {
                                editor.clear();
                            }
                            editor.apply();
                            startActivity(intent);
                            finish();
                        } else if (code == 401) {
                            ToastUtil.show(LoginActivity.this, "用户名或密码错误");
                        }
                        aiv_login.smoothToHide();
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_login:
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (username.equals("")) {
                    ToastUtil.show(this, "请输入用户名");
                    return;
                }
                if (password.equals("")) {
                    ToastUtil.show(this, "请输入密码");
                    return;
                }
                aiv_login.smoothToShow();
                login(username, password);
                break;
        }
    }
}